package com.mingyuan.library.domain.lending.patron.event;

import com.mingyuan.library.domain.lending.book.model.valueobject.BookId;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookType;
import com.mingyuan.library.domain.lending.librarybranch.model.valueobject.LibraryBranchId;
import com.mingyuan.library.domain.lending.patron.model.valueobject.CheckoutDuration;
import com.mingyuan.library.domain.lending.patron.model.valueobject.PatronId;
import lombok.NonNull;
import lombok.Value;

import java.time.Instant;
import java.util.UUID;

/**
 * 借阅图书信息
 *
 * @author yangzk
 * @date 2023/12/28 18:39
 **/
@Value
public class BookCheckedOut implements PatronEvent {
    /**
     * 事件ID
     */
    @NonNull
    UUID eventId = UUID.randomUUID();
    /**
     * 借阅的开始时间
     */
    @NonNull Instant when;
    /**
     * 读者ID
     */
    @NonNull UUID patronId;
    /**
     * 图书ID
     */
    @NonNull UUID bookId;
    /**
     * 图书类型
     */
    @NonNull BookType bookType;
    /**
     * 图书馆分馆ID
     */
    @NonNull UUID libraryBranchId;
    /**
     * 借阅的截止时间
     */
    @NonNull Instant till;

    /**
     * 获取当前借阅图书
     *
     * @param bookId           图书ID
     * @param bookType         图书类型
     * @param libraryBranchId  图书馆分馆ID
     * @param patronId         读者ID
     * @param checkoutDuration 借阅持续时间
     * @return 借阅图书
     * @author yangzk
     * @date 2023/12/28 18:37
     **/
    public static BookCheckedOut bookCheckedOutNow(BookId bookId, BookType bookType, LibraryBranchId libraryBranchId, PatronId patronId, CheckoutDuration checkoutDuration) {
        return new BookCheckedOut(
                Instant.now(),
                patronId.getPatronId(),
                bookId.getBookId(),
                bookType,
                libraryBranchId.getLibraryBranchId(),
                checkoutDuration.to());
    }
}